<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<base href="<%=basePath%>">
		<meta charset="utf-8" />
		<title></title>
		<meta name="description" content="overview & stats" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<link href="static/css/bootstrap.min.css" rel="stylesheet" />
		<link href="static/css/bootstrap-responsive.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="static/css/font-awesome.min.css" />
		<!-- 下拉框 -->
		<link rel="stylesheet" href="static/css/chosen.css" />
		<link rel="stylesheet" href="static/css/ace.min.css" />
		<link rel="stylesheet" href="static/css/ace-responsive.min.css" />
		<link rel="stylesheet" href="static/css/ace-skins.min.css" />
		<script type="text/javascript" src="static/js/jquery-1.7.2.js"></script>
		<!--提示框-->
		<script type="text/javascript" src="static/js/jquery.tips.js"></script>

		<script type="text/javascript" src="static/js/select2-4.0.3/js/select2.full.js"></script>
		<link rel="stylesheet" href="static/js/select2-4.0.3/css/select2.min.css"  media="all">

<script type="text/javascript">
	$(top.hangge());
	$(document).ready(function(){
       // debugger
		if($("#user_id").val()!=""){
			$("#loginname").attr("readonly","readonly");
			$("#loginname").css("color","gray");
		}
        var roleid = "${pd.ROLE_ID}"
       // console.log(roleid)

        if(roleid=="e148852b899e4714aa175e1306dbef32"|| roleid=="8516c618faff4a5fbf33a15f7e33a911"|| roleid=='${WWZY}'){//催收组长||催收员||委外专员
         if(roleid=="e148852b899e4714aa175e1306dbef32"){//催收组长
             var url="user/findAllManagers";
             // var args={"orgId":org_id};
             $.getJSON(url,function(data){
                // debugger
//                console.log("执行的是催收组长")
//                 console.log(data)
                 $("#parent_id option:not(:first)").remove();
                 for(var i=0;i<data.length;i++){
                     var city_id=data[i].USER_ID;
                     var city_name=data[i].NAME;
                     $("#parent_id").append("<option value='"+city_id+"'>"+city_name+"</option>");
                 }
                  <%--console.log("parent_id1="+"${pd.PARENT_ID}")--%>
                 <%--console.log("org_id1="+"${pd.ORGANIZATION_ID}")--%>
                 $("#parent_id").val("${pd.PARENT_ID}")
                 $("#org_id").val("${pd.ORGANIZATION_ID}")
             });
             //changgezhiwei();
         }
        if(roleid=="8516c618faff4a5fbf33a15f7e33a911"){//催收员
            var url = "user/findGroupLeaderByOrgId";
            var org_id ="${pd.ORGANIZATION_ID}"
            var args = {"orgId": org_id};
//            console.log("org_id："+org_id)
//            console.log("org_id")
            if($("select[mark='zhiwei']").val()=="8516c618faff4a5fbf33a15f7e33a911") {
                $.getJSON(url, args, function (data) {
//                    console.log("执行的是催收员初始化")
//                    console.log($("select[mark='zhiwei']").val())
                    console.log(data)
                    $("#parent_id option:not(:first)").remove();
                    for (var i = 0; i < data.length; i++) {
                        var city_id = data[i].USER_ID;
                        var city_name = data[i].NAME;
                        $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                    }
                    <%--console.log("parent_id2="+"${pd.PARENT_ID}")--%>
                    <%--console.log("org_id2="+"${pd.ORGANIZATION_ID}")--%>
                    $("#parent_id").val("${pd.PARENT_ID}")
                    $("#org_id").val("${pd.ORGANIZATION_ID}")
                });
            }
          //  changgezhiwei();
        }
            if(roleid=='${WWZY}'){//委外专员
                var url="user/findAllWWGSManagers";
                $.getJSON(url,function(data){
                console.log("执行的是委外专员")
                console.log(data)
                    $("#parent_id option:not(:first)").remove();
                    for(var i=0;i<data.length;i++){
                        var city_id=data[i].USER_ID;
                        var city_name=data[i].NAME;
                        $("#parent_id").append("<option value='"+city_id+"'>"+city_name+"</option>");
                    }
                    $("#org_id").attr("disabled", "disabled");
                    $("#parent_id").val("${pd.PARENT_ID}")

                });
            }

        }else {
            console.log("页面初始化禁用")
            $("#org_id").attr("disabled", "disabled");
            $("#parent_id").attr("disabled", "disabled");
            //changgezhiwei();
        }
        initSelect();//初始化启用不器用
        initSelect2();//初始化员工类型

    });

	function initSelect() {
        var data = [{ id: ' ', text: '请选择' }, { id: 0, text: '启用' }, { id: 1, text: '不启用' }];
		var select2Obj = $("#STATUS").select2({
			data: data
		});
        select2Obj.val("${pd.STATUS}").trigger("change");
        return select2Obj;
	}
	function initSelect2() {
        var data = [{ id: ' ', text: '请选择员工类型' }, { id: 0, text: '内催' }, { id: 1, text: '内催外包' }, { id: 2, text: '内催支援' }, { id: 3, text: '委外' }, { id: 4, text: '其他' }];
		var select2Obj = $("#employee_status").select2({
			data: data
		});
        select2Obj.val("${pd.employee_status}").trigger("change");
        return select2Obj;
	}

	function changgezhiwei(){
        console.log($("select[mark='zhiwei']").val())
        var zhiweiStr = $("select[mark='zhiwei']").val()
       if(zhiweiStr=="e148852b899e4714aa175e1306dbef32"){
//            console.log("催收组长")
           $("#org_id").removeAttr("disabled");
           $("#parent_id").removeAttr("disabled");
           $("#org_id").val("-1");
           $("#parent_id").val("-1");
           $("#parent_id  option:not(:first)").remove();
           var url="user/findAllManagers";
          // var args={"orgId":org_id};
           $.getJSON(url,function(data){
//               console.log("执行的是催收组长")
//               console.log(data)
               $("#parent_id option:not(:first)").remove();
               for(var i=0;i<data.length;i++){
                   var city_id=data[i].USER_ID;
                   var city_name=data[i].NAME;
                   $("#parent_id").append("<option value='"+city_id+"'>"+city_name+"</option>");
               }
           });


       }else if(zhiweiStr=="8516c618faff4a5fbf33a15f7e33a911"){
         //   console.log("催收员")
           $("#org_id").removeAttr("disabled");
           $("#parent_id").removeAttr("disabled");
           $("#org_id").val("-1");
           $("#parent_id").val("-1");
           $("#parent_id  option:not(:first)").remove();
           if($("select[mark='zhiwei']").val()=="8516c618faff4a5fbf33a15f7e33a911") {
//               console.log("催收员111")
//               console.log($("select[mark='zhiwei']").val())
               $("#org_id").change(function () {
                   //使#city只保留第一个option子节点
                   // $("#org_id option:not(:first)").remove();
                   var org_id = $(this).val();
//                   console.log("org_id:" + org_id)
                   if (org_id != "") {
                       var url = "user/findGroupLeaderByOrgId";
                       var args = {"orgId": org_id};
                       if($("select[mark='zhiwei']").val()=="8516c618faff4a5fbf33a15f7e33a911") {
                           $.getJSON(url, args, function (data) {
//                               console.log("执行的是催收员")
//                               console.log($("select[mark='zhiwei']").val())
//                               console.log(data)
                               $("#parent_id option:not(:first)").remove();
                               for (var i = 0; i < data.length; i++) {
                                   var city_id = data[i].USER_ID;
                                   var city_name = data[i].NAME;
                                   $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                               }
                           });
                       }
                   }
               });
           }
        }else if(zhiweiStr=='${WWZY}'){
          // debugger
              console.log("委外专员")
          // $("#org_id").removeAttr("disabled");
           $("#parent_id").removeAttr("disabled");
          // $("#org_id").val("-1");
           if(!$("#parent_id").val())
           { $("#parent_id").val("-1");}
           $("#parent_id  option:not(:first)").remove();
           if($("select[mark='zhiwei']").val()=='${WWZY}') {
               console.log("委外专员111")
               console.log($("select[mark='zhiwei']").val())
                       var url = "user/findAllWWGSManagers";
                       if($("select[mark='zhiwei']").val()=='${WWZY}') {
                           $.getJSON(url,function (data) {
                                 console.log("执行的是委外专员")
                                 console.log($("select[mark='zhiwei']").val())
                                console.log(data)
                               $("#parent_id option:not(:first)").remove();
                               for (var i = 0; i < data.length; i++) {
                                   var city_id = data[i].USER_ID;
                                   var city_name = data[i].NAME;
                                   $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                               }
                           });
                       }
           }
       } else {
//           console.log("其他")
           $("#org_id").attr("disabled","disabled");  ;
           $("#parent_id").attr("disabled","disabled");
           $("#org_id").val("-1");
           $("#parent_id").val("-1");
       }
    }
    function changgexiaozu(){
        var url = "user/findGroupLeaderByOrgId";
        var org_id = $("#org_id").val()
        var args = {"orgId": org_id};
        if($("select[mark='zhiwei']").val()=="8516c618faff4a5fbf33a15f7e33a911") {
            $.getJSON(url, args, function (data) {
                console.log(data)
                $("#parent_id option:not(:first)").remove();
                for (var i = 0; i < data.length; i++) {
                    var city_id = data[i].USER_ID;
                    var city_name = data[i].NAME;
                    $("#parent_id").append("<option value='" + city_id + "'>" + city_name + "</option>");
                }
            });
    }
    }
	//保存
	function save(){
		if($("#role_id").val()==""){

			$("#role_id").tips({
				side:3,
	            msg:'选择角色',
	            bg:'#AE81FF',
	            time:2
	        });

			$("#role_id").focus();
			return false;
		}
		if($("#loginname").val()=="" || $("#loginname").val()=="此用户名已存在!"){

			$("#loginname").tips({
				side:3,
	            msg:'输入用户名',
	            bg:'#AE81FF',
	            time:2
	        });

			$("#loginname").focus();
			$("#loginname").val('');
			$("#loginname").css("background-color","white");
			return false;
		}else{
			$("#loginname").val(jQuery.trim($('#loginname').val()));
		}

		if($("#NUMBER").val()==""){

			$("#NUMBER").tips({
				side:3,
	            msg:'输入编号',
	            bg:'#AE81FF',
	            time:3
	        });
			$("#NUMBER").focus();
			return false;
		}
		if($("#employee_status").val()==" "){

			$("#employee_status").tips({
				side:3,
	            msg:'输入员工类型',
	            bg:'#AE81FF',
	            time:3
	        });
			$("#employee_status").focus();
			return false;
		}
		if($("#user_id").val()=="" && $("#password").val()==""){

			$("#password").tips({
				side:3,
	            msg:'输入密码',
	            bg:'#AE81FF',
	            time:2
	        });

			$("#password").focus();
			return false;
		}
		if($("#password").val()!=$("#chkpwd").val()){

			$("#chkpwd").tips({
				side:3,
	            msg:'两次密码不相同',
	            bg:'#AE81FF',
	            time:3
	        });

			$("#chkpwd").focus();
			return false;
		}
		if($("#name").val()==""){

			$("#name").tips({
				side:3,
	            msg:'输入姓名',
	            bg:'#AE81FF',
	            time:3
	        });
			$("#name").focus();
			return false;
		}
        /**
		 * 验证分机号不重复
         */

//        if($("input[name='machineNum']").attr("repeated")!=undefined){
//            $("input[name='machineNum']").tips({
//                side:3,
//                msg:'该分机号已存在',
//                bg:'#AE81FF',
//                time:3
//            });
//            $("input[name='machineNum']").focus();
//            return false;
//		}

//		var myreg = /^(((13[0-9]{1})|159)+\d{8})$/;
//		if($("#PHONE").val()==""){
//
//			$("#PHONE").tips({
//				side:3,
//	            msg:'输入手机号',
//	            bg:'#AE81FF',
//	            time:3
//	        });
//			$("#PHONE").focus();
//			return false;
//		}else if($("#PHONE").val().length != 11 && !myreg.test($("#PHONE").val())){
//			$("#PHONE").tips({
//				side:3,
//	            msg:'手机号格式不正确',
//	            bg:'#AE81FF',
//	            time:3
//	        });
//			$("#PHONE").focus();
//			return false;
//		}
//
//		if($("#EMAIL").val()==""){
//
//			$("#EMAIL").tips({
//				side:3,
//	            msg:'输入邮箱',
//	            bg:'#AE81FF',
//	            time:3
//	        });
//			$("#EMAIL").focus();
//			return false;
//		}else if(!ismail($("#EMAIL").val())){
//			$("#EMAIL").tips({
//				side:3,
//	            msg:'邮箱格式不正确',
//	            bg:'#AE81FF',
//	            time:3
//	        });
//			$("#EMAIL").focus();
//			return false;
//		}

		if($("#user_id").val()==""){
			hasU();
		}else{
			$("#userForm").submit();
			$("#zhongxin").hide();
			$("#zhongxin2").show();
		}
	}

	function ismail(mail){
		return(new RegExp(/^(?:[a-zA-Z0-9]+[_\-\+\.]?)*[a-zA-Z0-9]+@(?:([a-zA-Z0-9]+[_\-]?)*[a-zA-Z0-9]+\.)+([a-zA-Z]{2,})+$/).test(mail));
		}

	//判断用户名是否存在
	function hasU(){
		var USERNAME = $("#loginname").val();
		var url = "<%=basePath%>user/hasU.do?USERNAME="+USERNAME+"&tm="+new Date().getTime();
		$.get(url,function(data){
			if(data=="error"){
				$("#loginname").css("background-color","#D16E6C");

				setTimeout("$('#loginname').val('此用户名已存在!')",500);

			}else{
				$("#userForm").submit();
				$("#zhongxin").hide();
				$("#zhongxin2").show();
			}
		});
	}

	//判断邮箱是否存在
	function hasE(USERNAME){
		var EMAIL = $("#EMAIL").val();
		var url = "<%=basePath%>user/hasE.do?EMAIL="+EMAIL+"&USERNAME="+USERNAME+"&tm="+new Date().getTime();
		$.get(url,function(data){
			if(data=="error"){

				$("#EMAIL").tips({
					side:3,
		            msg:'邮箱已存在',
		            bg:'#AE81FF',
		            time:3
		        });

				setTimeout("$('#EMAIL').val('')",2000);

			}
		});
	}

	//判断编码是否存在
	function hasN(USERNAME){
		var NUMBER = $("#NUMBER").val();
		var url = "<%=basePath%>user/hasN.do?NUMBER="+NUMBER+"&USERNAME="+USERNAME+"&tm="+new Date().getTime();
		$.get(url,function(data){
			if(data=="error"){

				$("#NUMBER").tips({
					side:3,
		            msg:'编号已存在',
		            bg:'#AE81FF',
		            time:3
		        });

				setTimeout("$('#NUMBER').val('')",2000);

			}
		});
	}



</script>
	</head>
<body>
<br>
	<form action="user/${msg }.do" name="userForm" id="userForm" method="post">
		<input type="hidden" name="USER_ID" id="user_id" value="${pd.USER_ID }"/>
		<div id="zhongxin" style="margin-left: 15px;">
		<table>

            <c:if test="${fx != 'head'}">
                <c:if test="${pd.ROLE_ID != '1'}">
                    <tr class="info" height="50px">
                        <td>
                            <select class="chzn-select" name="ROLE_ID" id="role_id" mark="zhiwei"
                                    onchange="changgezhiwei()" data-placeholder="请选择职位" style="vertical-align:top;">
                                <option value=""></option>
                                <c:forEach items="${roleList}" var="role">
                                    <option value="${role.ROLE_ID }"
                                            <c:if test="${role.ROLE_ID == pd.ROLE_ID }">selected</c:if>>${role.ROLE_NAME }</option>
                                </c:forEach>
                            </select>
                        </td>
                    </tr>
                </c:if>
                <c:if test="${pd.ROLE_ID == '1'}">
                    <input name="ROLE_ID" id="role_id" value="1" type="hidden"/>
                </c:if>
            </c:if>

            <c:if test="${fx == 'head'}">
                <input name="ROLE_ID" id="role_id" value="${pd.ROLE_ID }" type="hidden"/>
            </c:if>
            <tr height="50px">
                <td>
                    <select name="orgId" id="org_id" onchange="changgexiaozu()" value="${pd.ORGANIZATION_ID}">
                        <option value="-1">请选择小组</option>
                        <c:forEach items="${orglist }" var="orgnize">
                            <option value="${ orgnize.id }">${ orgnize.name}</option>
                        </c:forEach>
                    </select>
                </td>
            </tr>
            <tr height="30px" style="margin-bottom: 15px;">
                <td>
                    <select name="parentId" id="parent_id" value="${pd.PARENT_ID }">
                        <option value="-1">请选择上级领导</option>
                        <%--  <option value="0">张组长1</option>
                          <option value="1">张组长2</option>
                          <option value="2">张组长3</option>
                          <option value="3">张组长4</option>--%>
                    </select>
                </td>
            </tr>
            <tr>
                <td><input type="text" name="USERNAME" id="loginname" value="${pd.USERNAME }" maxlength="32"
                           placeholder="这里输入用户名" title="用户名"/></td>
            </tr>
            <tr>
                <td><input type="text" name="NUMBER" id="NUMBER" value="${pd.NUMBER }" maxlength="32"
                           placeholder="这里输入编号" title="编号" onblur="hasN('${pd.USERNAME }')"/></td>
            </tr>
            <tr>
                <td style="padding-bottom: 10px;">
                    <select id="STATUS" name="STATUS" title="是否启用"></select>
                </td>
            </tr>
            <tr>
                <td style="padding-bottom: 10px;">
                    <select id="employee_status" name="employee_status" title="员工类型"></select>
                </td>
            </tr>
            <c:if test="${pswd=='1'}">
                <tr>
                    <td><input type="password" name="PASSWORD" id="password" maxlength="32" placeholder="输入密码"
                               title="密码"/></td>
                </tr>
                <tr>
                    <td><input type="password" name="chkpwd" id="chkpwd" maxlength="32" placeholder="确认密码"
                               title="确认密码"/></td>
                </tr>
            </c:if>
            <tr>
                <td><input type="text" name="NAME" id="name" value="${pd.NAME }" maxlength="32" placeholder="这里输入姓名"
                           title="姓名"/></td>
            </tr>
            <tr>
                <td><input type="number" name="PHONE" id="PHONE" value="${pd.PHONE }" maxlength="32"
                           placeholder="这里输入手机号" title="手机号"/></td>
            </tr>
            <tr>
                <td><input type="email" name="EMAIL" id="EMAIL" value="${pd.EMAIL }" maxlength="32" placeholder="这里输入邮箱"
                           title="邮箱" onblur="hasE('${pd.USERNAME }')"/></td>
            </tr>
            <tr>
                <td><input type="text" name="machineNum" id="machineNum" value="${pd.machine_num }"
                           placeholder="这里输入分机号" maxlength="64" title="分机号"/></td>
            </tr>
            <tr>
                <td><input type="text" name="BZ" id="BZ" value="${pd.BZ }" placeholder="这里输入备注" maxlength="64"
                           title="备注"/></td>
            </tr>
            <tr>
                <td style="text-align: center;">
                    <a class="btn btn-mini btn-primary" onclick="save();">保存</a>
                    <a class="btn btn-mini btn-danger" onclick="top.Dialog.close();">取消</a>
                </td>
            </tr>
        </table>
    </div>

    <div id="zhongxin2" class="center" style="display:none"><br/><br/><br/><br/><img
            src="static/images/jiazai.gif"/><br/><h4 class="lighter block green"></h4></div>

</form>

<!-- 引入 -->
<script type="text/javascript">window.jQuery || document.write("<script src='static/js/jquery-1.9.1.min.js'>\x3C/script>");</script>
<script src="static/js/bootstrap.min.js"></script>
<script src="static/js/ace-elements.min.js"></script>
<script src="static/js/ace.min.js"></script>
<script type="text/javascript" src="static/js/chosen.jquery.min.js"></script><!-- 下拉框 -->

<script type="text/javascript">

    $(function () {

        //单选框
        $(".chzn-select").chosen();
        $(".chzn-select-deselect").chosen({allow_single_deselect: true});

        //日期框
//			$('.date-picker').datepicker();

//            $("input[name='machineNum']")
//				.blur(function() {
//					var currentInput = $(this), machineNum = currentInput.val();
//						if ($("input[name='machineNum']").attr("repeated") == undefined){
//							$.get("user/cntMachineNum", {machineNum: machineNum}, function (data) {
//								if($("input[name='USER_ID']").val() == undefined){//新增
//									if (data != 0) {//分机号不能重复 新增
//										currentInput.attr("repeated", "repeated");//设置分机号重复状态
//										return;
//									}
//								}else {//编辑
//									if (data >= 1 && currentInput.attr("oldVal") != currentInput.val()) {//分机号不能重复 新增
//										currentInput.attr("repeated", "repeated");//设置分机号重复状态
//										return;
//									}
//								}
//								currentInput.removeAttr("repeated");
//							});
//						}
//				})
//				.focusin(function(){
//                    var currentInput = $(this);
//                    if(currentInput.attr("oldVal") != undefined) return;
//                    currentInput.attr("oldVal", currentInput.val());
//				})
//				.keyup(function(){
//					var currentInput = $(this);
//					currentInput.removeAttr("repeated");
//				});
    });

</script>

</body>
</html>